package com.boarsoft.boar.batch;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.boar.batch.service.BatchCallback;
import com.boarsoft.boar.batch.service.BatchInfoService;
import com.boarsoft.common.util.InetUtil;
import com.boarsoft.flow.core.SimpleFlow;
import com.boarsoft.flow.core.bean.BaseNode;
import com.boarsoft.flow.event.SimpleFlowListener;
import com.boarsoft.serialize.TaggedJsonSerializer;

public class BatchFlowListener implements SimpleFlowListener {
	private static Logger log = LoggerFactory.getLogger(BatchFlowListener.class);

	protected BatchCallback batchCallback;

	protected BatchInfoService batchInfoSvc;

	@Override
	public void onEnd(SimpleFlow flow) {
		if (flow.hasParent()) {
			log.info("Subflow {} end, parent = {}", flow.getId(), flow.getParent());
		} else {
			batchCallback.onSuccess(flow.getId(), InetUtil.getAddr());
		}
	}

	@Override
	public void onError(SimpleFlow flow) {
		if (flow.hasParent()) {
			log.info("Subflow {} error, parent = {}", flow.getId(), flow.getParent());
		} else {
			try {
				batchCallback.onFailed(flow.getId(), flow.getStep(), InetUtil.getAddr(), //
						flow.getError().getMessage(), TaggedJsonSerializer.serialize(flow.getData()));
			} catch (IOException e) {
				log.error("Error on callback onFailed", e);
			}
		}
	}

	@Override
	public void onSuspend(SimpleFlow flow) {
		if (flow.hasParent()) {
			log.info("Subflow {} suspend, parent = {}", flow.getId(), flow.getParent());
		} else {
			batchCallback.onSuspend(flow.getId(), flow.getStep(), InetUtil.getAddr());
		}
	}

	@Override
	public void onStop(SimpleFlow flow) {
		if (flow.hasParent()) {
			log.info("Subflow {} stop, parent = {}", flow.getId(), flow.getParent());
		} else {
			batchCallback.onStop(flow.getId(), flow.getStep(), InetUtil.getAddr());
		}
	}

	@Override
	public void onTimeout(SimpleFlow flow) {
		if (flow.hasParent()) {
			log.info("Subflow {} timeout, parent = {}", flow.getId(), flow.getParent());
		} else {
			batchCallback.onTimeout(flow.getId(), flow.getStep(), InetUtil.getAddr());
		}
	}

	@Override
	public void onBegin(SimpleFlow flow) {
		if (flow.hasParent()) {
			log.info("Subflow {} begin, parent = {}", flow.getId(), flow.getParent());
		} else {
			batchCallback.onBegin(flow.getId(), InetUtil.getAddr());
		}
	}

	@Override
	public void onBeforeStep(SimpleFlow flow, Object data, BaseNode next) {
		batchCallback.onStep(flow.getId(), next == null ? null : next.getId(), InetUtil.getAddr());
	}

	// -------------

	public BatchCallback getBatchCallback() {
		return batchCallback;
	}

	public void setBatchCallback(BatchCallback batchCallback) {
		this.batchCallback = batchCallback;
	}

	@Override
	public void onAfterStep(SimpleFlow flow, Object data, Throwable flowEx, String next, Throwable stepEx, long time) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBeforeHandle(SimpleFlow flow, Object data, Throwable flowEx, Object dd) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAfterHandle(SimpleFlow flow, Object data, Throwable flowEx, Object checkInData, Object dataCheckOut,
			Throwable handleEx) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onFailHandle(SimpleFlow flow, Object data, Throwable flowEx, Object checkInData, Throwable handleEx) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onFailStep(SimpleFlow flow, Object data, Throwable flowEx, String step, Throwable stepEx) {
		// TODO Auto-generated method stub

	}

}